package com.qys.livesMall.app.user.controller.sys;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.app.user.model.dto.AppBaseDTO;
import com.qys.livesMall.app.user.model.dto.app.AppUserAddressDTO;
import com.qys.livesMall.app.user.model.dto.sys.SysUserAddressDTO;
import com.qys.livesMall.app.user.model.dto.sys.SysUserAddressPageDTO;
import com.qys.livesMall.app.user.model.vo.sys.H2AppUserAddressVO;
import com.qys.livesMall.app.user.service.IH2AppUserAddressService;
import com.qys.livesMall.common.result.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 用户收货地址 前端控制器
 * </p>
 *
 * @author dp
 * @since 2023-04-24
 */
@RestController
@RequestMapping("/sys/user/address")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "sys-用户收货地址")
public class SysH2AppUserAddressController {

    private final IH2AppUserAddressService userAddressService;

    @PostMapping("/add")
    @Operation(summary = "添加地址信息")
    public R<String> addAddressInfo(@RequestBody @Validated AppUserAddressDTO addressDTO) {
        return R.ok(userAddressService.save(addressDTO, addressDTO.getUserId()));
    }

    @PutMapping("/edit")
    @Operation(summary = "修改地址信息")
    public R<Void> editAddressInfo(@RequestBody @Validated AppUserAddressDTO addressDTO) {
        userAddressService.save(addressDTO, addressDTO.getUserId());
        return R.ok();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除地址信息")
    public R<Void> remove(@PathVariable String id) {
        userAddressService.removeById(id);
        return R.ok();
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询地址信息")
    public R<Page<H2AppUserAddressVO>> page(@RequestBody SysUserAddressPageDTO queryDTO) {
        return R.ok(userAddressService.page(queryDTO));
    }

    @PostMapping("/default")
    @Operation(summary = "设置默认地址")
    public R<Void> setDefaultAddress(@RequestBody SysUserAddressDTO dto) {
        userAddressService.setDefaultAddressByUserId(dto);
        return R.ok();
    }
}
